package org.example.storage;

import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * 通用存储 - 支持基础CRUD操作
 */
public class GenericStorage<K, V> {
    protected final ConcurrentMap<K, V> storage;

    public GenericStorage() {
        this.storage = new ConcurrentHashMap<>();
    }

    public GenericStorage(int initialCapacity) {
        this.storage = new ConcurrentHashMap<>(initialCapacity);
    }

    /**
     * 存储数据
     */
    public void put(K key, V value) {
        storage.put(key, value);
    }

    /**
     * 获取数据
     */
    public Optional<V> get(K key) {
        return Optional.ofNullable(storage.get(key));
    }

    /**
     * 删除数据
     */
    public boolean remove(K key) {
        return storage.remove(key) != null;
    }

    /**
     * 判断是否存在
     */
    public boolean contains(K key) {
        return storage.containsKey(key);
    }

    /**
     * 获取存储大小
     */
    public int size() {
        return storage.size();
    }

    /**
     * 清理所有数据
     */
    public void clear() {
        storage.clear();
    }

    /**
     * 获取所有键
     */
    public java.util.Set<K> keySet() {
        return storage.keySet();
    }

    /**
     * 获取所有值
     */
    public java.util.Collection<V> values() {
        return storage.values();
    }

    /**
     * 获取所有条目
     */
    public java.util.Set<java.util.Map.Entry<K, V>> entrySet() {
        return storage.entrySet();
    }
}
